package be.ac.ua.comp.projectGPT.players;

//~--- non-JDK imports --------------------------------------------------------

import be.ua.ac.comp.projectGPT.auctionPolicies.AuctionPolicy;
import be.ac.ua.comp.projectGPT.BiddingPolicies.BiddingPolicy;
import be.ac.ua.comp.projectGPT.auctioneers.EnglishAuctioneer;
import be.ac.ua.comp.projectGPT.auctions.Auction;
import be.ac.ua.comp.projectGPT.auctions.EnglishAuction;
import be.ac.ua.comp.projectGPT.exceptions.InitializationException;
import java.util.concurrent.ArrayBlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//~--- JDK imports ------------------------------------------------------------

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Market {
    int             id     = 0;
    Logger          logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
    ThreadPoolExecutor executor;
            final ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(
            5);

    public Market() {
        logger.info("Creating");
        executor = new ThreadPoolExecutor(2,10,10,TimeUnit.SECONDS,queue);
    }

    public void startAuction(Auction auction) {
        logger.info("Starting auction");
        executor.execute(auction);
    }

    public void endAuctions() {
        executor.shutdown();
    }

    public EnglishAuction getEnglishAuction(String auctioneerName, AuctionPolicy auctionPolicy,
            BiddingPolicy biddingPolicy)
            throws InitializationException {
        EnglishAuction    englishAuction    = new EnglishAuction();
        biddingPolicy.setAuction(englishAuction);
        auctionPolicy.setAuction(englishAuction);
        EnglishAuctioneer englishAuctioneer = new EnglishAuctioneer(auctioneerName, englishAuction);

        logger.info("Setting bidding policy for English Auctioneer " + englishAuctioneer.getName() + " to "
                    + biddingPolicy.getClass().getSimpleName());
        biddingPolicy.setAuctioneer(englishAuctioneer);
        englishAuctioneer.setBiddingPolicy(biddingPolicy);
        englishAuction.setAuctioneer(englishAuctioneer);
        logger.info("Setting auction policy for English auction to " + auctionPolicy.getClass().getSimpleName());
        englishAuction.setPolicy(auctionPolicy);

        return englishAuction;
    }
}
